﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using TS2015.Models;
using System.Web.Script.Serialization;

namespace TS2015.Areas.Admin.Controllers
{
    public class RegistrationManagementController : Controller
    {
        TS2015Context db = new TS2015Context();
        // GET: Admin/RegistrationManagement
        #region TrainingSystem
        public JsonResult GetTrainingSystem()
        {
            return Json(db.TbHeDaoTaos.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult TrainingSystem()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadTrainingSystem([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbHeDaoTaos.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult EditingPopup_CreateTrainingSystem([DataSourceRequest] DataSourceRequest request, TbHeDaoTao data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbHeDaoTaos.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateTrainingSystem([DataSourceRequest] DataSourceRequest request, TbHeDaoTao data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyTrainingSystem([DataSourceRequest] DataSourceRequest request, TbHeDaoTao data)
        {
            if (data != null)
            {
                TbHeDaoTao itemdata = db.TbHeDaoTaos.First(item => item.MaHeDaoTao == data.MaHeDaoTao);
                db.TbHeDaoTaos.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        #region Department
        //[OutputCache(Duration=600)]
        public JsonResult GetDepartment()
        {
            return Json(db.TbKhoas.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Department()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadDepartment([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbKhoas.ToDataSourceResult(request));
        }

        public ActionResult EditingPopup_CreateDepartment([DataSourceRequest] DataSourceRequest request, TbKhoa data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbKhoas.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }

        public ActionResult EditingPopup_UpdateDepartment([DataSourceRequest] DataSourceRequest request, TbKhoa data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyDepartment([DataSourceRequest] DataSourceRequest request, TbKhoa data)
        {
            if (data != null)
            {
                TbKhoa itemdata = db.TbKhoas.First(item => item.MaKhoa == data.MaKhoa);
                db.TbKhoas.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        //[ChildActionOnly]
        [HttpPost]
        [ActionName("Insert")]
        public ActionResult Insert1()
        {
            return PartialView();
        }
        public ActionResult Insert()
        {
            return PartialView();
        }
        #region Phylum
        public JsonResult GetPhylum()
        {
            return Json(db.TbNganhs.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Phylum()
        {
            // Lấy danh sách khoa để tham chiếu khóa ngoại
            ViewBag.TbKhoa = db.TbKhoas.ToList();

            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadPhylum([DataSourceRequest] DataSourceRequest request)
        {
            var data = db.TbNganhs;

            return Json(data.ToDataSourceResult(request));
        }

        public ActionResult EditingPopup_CreatePhylum([DataSourceRequest] DataSourceRequest request, TbNganh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbNganhs.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdatePhylum([DataSourceRequest] DataSourceRequest request, TbNganh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyPhylum([DataSourceRequest] DataSourceRequest request, TbNganh data)
        {
            if (data != null)
            {
                TbNganh itemdata = db.TbNganhs.First(item => item.MaNganh == data.MaNganh);
                db.TbNganhs.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }

        #endregion
        #region Major
        public JsonResult GetMajor()
        {
            return Json(db.TbChuyenNganhs.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Major()
        {
            // Lấy danh sách khoa để tham chiếu khóa ngoại
            ViewBag.TbNganh = db.TbNganhs.ToList();

            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadMajor([DataSourceRequest] DataSourceRequest request)
        {
            var data = db.TbChuyenNganhs;

            return Json(data.ToDataSourceResult(request));
        }

        public ActionResult EditingPopup_CreateMajor([DataSourceRequest] DataSourceRequest request, TbChuyenNganh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbChuyenNganhs.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateMajor([DataSourceRequest] DataSourceRequest request, TbChuyenNganh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyMajor([DataSourceRequest] DataSourceRequest request, TbChuyenNganh data)
        {
            if (data != null)
            {
                TbChuyenNganh itemdata = db.TbChuyenNganhs.First(item => item.MaChuyenNganh == data.MaChuyenNganh);
                db.TbChuyenNganhs.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        #region Group
        public JsonResult GetGroup()
        {
            return Json(db.TbKhoiThis.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Group()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadGroup([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbKhoiThis.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult EditingPopup_CreateGroup([DataSourceRequest] DataSourceRequest request, TbKhoiThi data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbKhoiThis.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateGroup([DataSourceRequest] DataSourceRequest request, TbKhoiThi data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyGroup([DataSourceRequest] DataSourceRequest request, TbKhoiThi data)
        {
            if (data != null)
            {
                TbKhoiThi itemdata = db.TbKhoiThis.First(item => item.MaKhoiThi == data.MaKhoiThi);
                db.TbKhoiThis.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        #region Area
        public JsonResult GetArea()
        {
            return Json(db.TbKhuVucs.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Area()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadArea([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbKhuVucs.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult EditingPopup_CreateArea([DataSourceRequest] DataSourceRequest request, TbKhuVuc data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbKhuVucs.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateArea([DataSourceRequest] DataSourceRequest request, TbKhuVuc data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyArea([DataSourceRequest] DataSourceRequest request, TbKhuVuc data)
        {
            if (data != null)
            {
                TbKhuVuc itemdata = db.TbKhuVucs.First(item => item.MaKhuVuc == data.MaKhuVuc);
                db.TbKhuVucs.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        #region Ethnic
        public JsonResult GetEthnic()
        {
            return Json(db.TbDanTocs.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Ethnic()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadEthnic([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbDanTocs.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult EditingPopup_CreateEthnic([DataSourceRequest] DataSourceRequest request, TbDanToc data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbDanTocs.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateEthnic([DataSourceRequest] DataSourceRequest request, TbDanToc data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyEthnic([DataSourceRequest] DataSourceRequest request, TbDanToc data)
        {
            if (data != null)
            {
                TbDanToc itemdata = db.TbDanTocs.First(item => item.MaDanToc == data.MaDanToc);
                db.TbDanTocs.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        #region Province
        //[OutputCache(Duration=600)]
        public JsonResult GetProvince()
        {
            return Json(db.TbTinhs.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Province()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadProvince([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbTinhs.ToDataSourceResult(request));
        }

        public ActionResult EditingPopup_CreateProvince([DataSourceRequest] DataSourceRequest request, TbTinh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbTinhs.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }

        public ActionResult EditingPopup_UpdateProvince([DataSourceRequest] DataSourceRequest request, TbTinh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyProvince([DataSourceRequest] DataSourceRequest request, TbTinh data)
        {
            if (data != null)
            {
                TbTinh itemdata = db.TbTinhs.First(item => item.MaTinh == data.MaTinh);
                db.TbTinhs.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        #region Commune

        public ActionResult Commune()
        {
            Database.SetInitializer<TS2015Context>(null);
            // Lấy danh sách khoa để tham chiếu khóa ngoại
            ViewBag.TbTinh = db.TbTinhs.ToList();

            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadCommune([DataSourceRequest] DataSourceRequest request)
        {
            var data = db.TbHuyens;

            return Json(data.ToDataSourceResult(request));
        }

        public ActionResult EditingPopup_CreateCommune([DataSourceRequest] DataSourceRequest request, TbHuyen data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbHuyens.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateCommnue([DataSourceRequest] DataSourceRequest request, TbHuyen data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyCommune([DataSourceRequest] DataSourceRequest request, TbHuyen data)
        {
            if (data != null)
            {
                TbHuyen itemdata = db.TbHuyens.First(item => item.MaHuyen == data.MaHuyen);
                db.TbHuyens.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        #region PriorityGroup
        public JsonResult GetPriorityGroup()
        {
            return Json(db.TbDoiTuongUuTiens.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult PriorityGroup()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadPriorityGroup([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbDoiTuongUuTiens.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult EditingPopup_CreatePriorityGroup([DataSourceRequest] DataSourceRequest request, TbDoiTuongUuTien data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbDoiTuongUuTiens.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdatePriorityGroup([DataSourceRequest] DataSourceRequest request, TbDoiTuongUuTien data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyPriorityGroup([DataSourceRequest] DataSourceRequest request, TbDoiTuongUuTien data)
        {
            if (data != null)
            {
                TbDoiTuongUuTien itemdata = db.TbDoiTuongUuTiens.First(item => item.MaDoiTuong == data.MaDoiTuong);
                db.TbDoiTuongUuTiens.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
        #region GraduateSubject
        public JsonResult GetGraduateSubject()
        {
            return Json(db.TbMonTotNghieps.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult GraduateSubject()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadGraduateSubject([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbMonTotNghieps.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult EditingPopup_CreateGraduateSubject([DataSourceRequest] DataSourceRequest request, TbMonTotNghiep data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbMonTotNghieps.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateGraduateSubject([DataSourceRequest] DataSourceRequest request, TbMonTotNghiep data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyGraduateSubject([DataSourceRequest] DataSourceRequest request, TbMonTotNghiep data)
        {
            if (data != null)
            {
                TbMonTotNghiep itemdata = db.TbMonTotNghieps.First(item => item.MaMon == data.MaMon);
                db.TbMonTotNghieps.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        #endregion
    }
}